package com.example.springmacexercise.demo.model.method;

import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@ResponseBody
@Controller
@RequestMapping("/art")
public class Demo1 {
    @RequestMapping("/param9")
    public String param9(String name, @RequestPart("myfile")MultipartFile file)throws
            IOException {
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filePath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+
                "/"+ UUID.randomUUID()+fileName;
        file.transferTo(new File(filePath));
        return filePath+"上传成功";
    }

    @RequestMapping("/param10")
    public String param10(HttpServletResponse response, HttpServletRequest request){
        String name = request.getParameter("name");
        Cookie[] cookies = request.getCookies();
        return name+"你好";
    }

    @RequestMapping("/header")
    @ResponseBody
    public String header (@RequestHeader("User-Agent") String userAgent){
        return "userAAgent"+userAgent;
    }


    @RequestMapping("/setsess")
    @ResponseBody
    public String setsess(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        if(session!=null){
            session.setAttribute("username","java");
        }
        return "session 存储成功";
    }

//    @RequestMapping("/sess")
//    @ResponseBody
//    public String sess(HttpServletRequest request){
//    }


}
